package cn.web.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import cn.web.dao.PropertyMapper;
import cn.web.entity.PageResult;
import cn.web.pojo.shop.Property;
import cn.web.service.shop.PropertyService;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class PropertyServiceImpl implements PropertyService {

    @Autowired
    private PropertyMapper propertyMapper;

    /**
     * 返回全部记录
     * @return
     */
    public List<Property> findAll() {
        return propertyMapper.selectAll();
    }

    /**
     * 分页查询
     * @param page 页码
     * @param size 每页记录数
     * @return 分页结果
     */
    public PageResult<Property> findPage(int page, int size) {
        PageHelper.startPage(page,size);
        Page<Property> propertys = (Page<Property>) propertyMapper.selectAll();
        return new PageResult<Property>(propertys.getTotal(),propertys.getResult());
    }

    /**
     * 条件查询
     * @param searchMap 查询条件
     * @return
     */
    public List<Property> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return propertyMapper.selectByExample(example);
    }

    /**
     * 分页+条件查询
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    public PageResult<Property> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page,size);
        Example example = createExample(searchMap);
        Page<Property> propertys = (Page<Property>) propertyMapper.selectByExample(example);
        return new PageResult<Property>(propertys.getTotal(),propertys.getResult());
    }

    /**
     * 根据Id查询
     * @param id
     * @return
     */
    public Property findById(String id) {
        return propertyMapper.selectByPrimaryKey(id);
    }

    /**
     * 新增
     * @param property
     */
    public void add(Property property) {
        propertyMapper.insert(property);
    }

    /**
     * 修改
     * @param property
     */
    public void update(Property property) {
        propertyMapper.updateByPrimaryKeySelective(property);
    }

    /**
     *  删除
     * @param id
     */
    public void delete(String id) {
        propertyMapper.deleteByPrimaryKey(id);
    }

    /**
     * 构建查询条件
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(Property.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // 商品基本属性id（管理员维护该表）
            if(searchMap.get("id")!=null && !"".equals(searchMap.get("id"))){
                criteria.andLike("id","%"+searchMap.get("id")+"%");
            }
            // 属性名
            if(searchMap.get("name")!=null && !"".equals(searchMap.get("name"))){
                criteria.andLike("name","%"+searchMap.get("name")+"%");
            }
            // 所属分类id
            if(searchMap.get("catagoryId")!=null && !"".equals(searchMap.get("catagoryId"))){
                criteria.andLike("catagoryId","%"+searchMap.get("catagoryId")+"%");
            }

            // 是否必填（0否1是）
            if(searchMap.get("isRequired")!=null ){
                criteria.andEqualTo("isRequired",searchMap.get("isRequired"));
            }

        }
        return example;
    }

}
